﻿using System;
using System.Collections.Generic;
using WordXmlPart = Microsoft.Office.Core.CustomXMLPart;
using WordXmlParts = Microsoft.Office.Core.CustomXMLParts;
using Core = Microsoft.Office.Core;
using System.Collections.Specialized;
using System.IO;

namespace CodeCounsel.Office.DatabindingToolkit
{
    public class CustomXmlPartCollection
        : IEnumerable<CustomXmlPart>, INotifyCollectionChanged
    {
        WordXmlParts _wordXmlParts = null;
        List<CustomXmlPart> _xmlParts = null;
        Document _owner = null;

        public CustomXmlPartCollection(Document owner, WordXmlParts wordXmlParts)
        {
            _owner = owner;
            _wordXmlParts = wordXmlParts;
            _xmlParts = new List<CustomXmlPart>();
            foreach (WordXmlPart wordXmlPart in wordXmlParts)
            {
                AddXmlPart(wordXmlPart);
            }
            _wordXmlParts.PartAfterAdd += new Core._CustomXMLPartsEvents_PartAfterAddEventHandler(WordXmlParts_PartAfterAdd);
            _wordXmlParts.PartBeforeDelete += new Core._CustomXMLPartsEvents_PartBeforeDeleteEventHandler(WordXmlParts_PartBeforeDelete);
        }

        public IEnumerator<CustomXmlPart> GetEnumerator()
        {
            foreach (CustomXmlPart part in _xmlParts)
            {
                if (part.WordXmlPart.BuiltIn == false ||
                    _owner.ShowBuiltInXmlParts)
                {
                    yield return part;
                }
            }
        }

        public CustomXmlPart Add(string xml)
        {
            WordXmlPart wordXmlPart = _wordXmlParts.Add(xml, Type.Missing);
            CustomXmlPart xmlPart = null;
            foreach (CustomXmlPart searchXmlPart in _xmlParts)
            {
                if (searchXmlPart.WordXmlPart == wordXmlPart)
                {
                    xmlPart = searchXmlPart;
                    break;
                }
            }
            return xmlPart;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<CustomXmlPart>)this).GetEnumerator();
        }

        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler handler = CollectionChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        void AddXmlPart(WordXmlPart wordXmlPart)
        {
            _xmlParts.Add(new CustomXmlPart(wordXmlPart));
            OnCollectionChanged(
                new NotifyCollectionChangedEventArgs(
                    NotifyCollectionChangedAction.Reset));
        }

        void RemovePart(WordXmlPart wordXmlPart)
        {
            CustomXmlPart xmlPart = null;
            foreach (CustomXmlPart searchPart in _xmlParts)
            {
                if (searchPart.WordXmlPart == wordXmlPart)
                {
                    xmlPart = searchPart;
                    break;
                }
            }
            if (xmlPart != null)
            {
                _xmlParts.Remove(xmlPart);
            }
        }


        void WordXmlParts_PartAfterAdd(WordXmlPart wordXmlPart)
        {
            AddXmlPart(wordXmlPart);
        }

        void WordXmlParts_PartBeforeDelete(WordXmlPart wordXmlPart)
        {
            RemovePart(wordXmlPart);
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;
    }

}
